import Vue from 'vue'
import App from './App'
import utils from './utils/utils'
import router from './utils/router'
import * as urlPath from './utils/urlPath'
import * as filters from '@/static/video/js/filter.js'

import uniLoadMore from '@/componets/uni/uni-load-more.vue'
import RouteParams from '@/mixin/RouteParams'
import userInfo from '@/data/UserInfo'
import {
	request,
	request_news,
	
} from "@/request/index.js"
// import BottomNavigationBar from '@/componets/BottomNavigationBar.vue'
// import PopUpLogin from '@/componets/PopUpLogin.vue'
import store from './store/index.js'
import {
	util 
} from '@/util/util.js'; // 挂载工具包
import {
	api
} from '@/api/api.js'; // api

Vue.prototype.$web_http = request;
Vue.prototype.$store = store
// 挂载全局方法
Vue.config.productionTip = false
Vue.prototype.util = util;
Vue.prototype.alert = util.alert;
Vue.prototype.request = util.request;
Vue.prototype.website = "https://api.youdaike.com/";
Vue.prototype.website1 = "https://api.youdaike.com/";
Vue.prototype.$api = api;
Vue.prototype.$web_api_http = request_news;
// import PopBindPhone from '@/componets/PopBindPhone.vue'
import {
	staticImgBaseIP
} from 'utils/urlPath.js'

import common from './utils/common.js'

import BottomNavigationBar from '@/componets/BottomNavigationBar.vue'



Vue.component('LoadMore', uniLoadMore)
Vue.component('BottomNavigationBar', BottomNavigationBar)

// Vue.component('PopUpLogin', PopUpLogin)
// Vue.component('PopBindPhone', PopBindPhone)
Vue.mixin(RouteParams)

Vue.config.productionTip = false
Vue.prototype.$utils = utils
Vue.prototype.$mRouter = router
Vue.prototype.$urlPath = urlPath
Vue.prototype.$userInfo = userInfo
Vue.prototype.$ids = {
	identity: uni.getStorageSync('identity') || 2,
	storeId: uni.getStorageSync('storeId') || 2
}
Vue.prototype.$staticImgBaseIP = staticImgBaseIP
// #ifdef MP-WEIXIN
Vue.prototype.$menuInfo = uni.getMenuButtonBoundingClientRect()
// #endif
Vue.prototype.$http = function(url, params = {}, loadingTip, onRequestSuccess, onRequestFail, onRequestComplete) {
	if (loadingTip !== null) {
		// uni.showLoading({
		// 	title: loadingTip || '正在加载…'
		// })
	}
	if (userInfo.isLogin()) {
		params.token = userInfo.getToken()
	}
	uni.request({
		header: {
			'content-type': 'application/x-www-form-urlencoded'
		},
		url: urlPath.baseUrl + url,
		data: params,
		method: 'POST',
		success: (res) => {
			if (res.statusCode === 401) {
				if (url == 'video/attentionList') {
					return;
				}
				if (url == "order/orderList") {
					uni.setStorageSync('fromPage', 'my');
					//如果是订单页面401 则跳转到登录页面
					uni.navigateTo({
						url: '/pagesB/login/Login'
					})
					return;
				} else {
					//其他页面打开弹窗登录
					this.$pushToLogin()
					return;
				}

				onRequestFail(401, 'token校验失败')
			} else {
				if (res && res.statusCode === 200) {
					if (res.data.code === 1) {
						onRequestSuccess(res.data)
					} else {
						onRequestFail(0, res.data.msg)
					}
				} else {
					onRequestFail(500, '请求失败')
				}
			}
		},
		fail: () => {
			onRequestFail()
		},
		complete: () => {
			if (onRequestComplete) {
				onRequestComplete()
			}
			if (loadingTip !== null) {
				uni.hideLoading()
			}
		}
	})
}

Vue.prototype.$upFile = function(url, files, data, method) {
	if (userInfo.isLogin()) {
		data['token'] = userInfo.getToken();
	}
	data['content-type'] = 'multipart/form-data';
	return uni.uploadFile({
		url: urlPath.baseUrl + url,
		filePath: files,
		name: 'file',
		// formData: data,
		header: data
	});
}

Vue.prototype.$noMultipleClicks = common.noMultipleClicks;
Vue.prototype.$colud = function() {
	return urlPath.cloudUrl;
}

Vue.prototype.$toast = function(msg) {
	if (msg) {
		this.$nextTick(() => {
			uni.showToast({
				icon: "none",
				title: msg,
				duration: 2000
			})
		})
	}
}
Vue.prototype.$push = function(url) {
	if (url) {
		if (router.auth(url)) {
			auth()
		} else {
			uni.navigateTo({
				url: url
			})
		}
	} else {
		uni.reLaunch({
			url: '/pages/index/index.vue'
		})
	}
}

Vue.prototype.$replace = function(url) {
	if (url) {
		if (router.auth(url)) {
			auth()
		} else {
			uni.redirectTo({
				url: url
			})
		}
	} else {
		uni.reLaunch({
			url: '/pages/index/index.vue'
		})
	}
}

Vue.prototype.$redirectToLogin = function(restorePage = '') {
	if (restorePage) {
		getApp().globalData.backPage = restorePage
	}
	//#ifdef H5
	var ua = window.navigator.userAgent.toLowerCase();
	//通过正则表达式匹配ua中是否含有MicroMessenger字符串
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		uni.redirectTo({
			url: '/pagesB/login/h5Login'
		})
	} else {
		// uni.redirectTo({
		// 	url: '/pagesB/login/Login'
		// })
		uni.$emit('popupLoginShow', true)
	}

	//#endif
	//#ifndef H5
	// uni.redirectTo({
	// 	url: '/pagesB/login/Login'
	// })
	uni.$emit('popupLoginShow', true)
	//#endif
}

Vue.prototype.$pushToLogin = function(restorePage = '') {
	if (restorePage) {
		getApp().globalData.backPage = restorePage
	}
	//#ifdef H5
	var ua = window.navigator.userAgent.toLowerCase();
	//通过正则表达式匹配ua中是否含有MicroMessenger字符串
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		uni.navigateTo({
			url: '/pagesB/login/h5Login'
		})
	} else {
		// uni.navigateTo({
		// 	url: '/pagesB/login/Login'
		// })
		uni.$emit('popupLoginShow', true)
	}

	//#endif
	//#ifndef H5
	// uni.navigateTo({
	// 	url: '/pagesB/login/Login'
	// })
	uni.$emit('popupLoginShow', true)
	//#endif
}

/**
 * 视频列表点赞功能
 * @param {Object} item
 */
Vue.prototype.$videoChangeLike = function(it) {
	if (!this.$userInfo.isLogin()) {
		this.$pushToLogin('back')
		return
	}
	this.$http("video/like", {
		video: it.videoId
	}, null, (res) => {
		if (res.data == "2") {
			it.isLike = 0;
			it.like_num -= 1;
		} else {
			it.isLike = 1;
			it.like_num += 1;
		}
	}, (errorCode, error) => {
		this.$toast(error)
	})
}

/**
 * 视频列表收藏功能
 *
 */
Vue.prototype.$videoChangeCollect = function(it) {
	if (!this.$userInfo.isLogin()) {
		this.$pushToLogin('back')
		return
	}
	this.$http("video/collect", {
		video: it.videoId
	}, null, (res) => {
		if (res.data == "2") {
			it.isCollect = 0;
			it.collect -= 1;
		} else {
			it.isCollect = 1;
			it.collect += 1;
		}
	}, (errorCode, error) => {
		this.$toast(error)
	})
}

Vue.prototype.$toUsercenter = function(userId) { // 跳转个人中心

	let userinfo = uni.getStorageSync('userinfo')

	if (userId == userinfo.id) {
		uni.navigateTo({
			url: "/pagesC/mine/my"
		})
	} else {

		uni.navigateTo({
			url: "/pagesA/mine/mineOther?userId=" + userId + "&circleId=1"
		})

	}

}

Vue.prototype.$toTopic = function(topic) { // 跳转话题中心
	uni.navigateTo({
		url: "/pagesC/video/topic?id=" + topic
	})
}

Vue.prototype.$toScenery = function(scenery) { // 跳转商家景点
	uni.navigateTo({
		url: "/pagesC/video/scenery?id=" + scenery
	})
}

Vue.prototype.$changeAttention = function(data) { // 关注用户、取消关注
	if (!this.$userInfo.isLogin()) {
		this.$pushToLogin('back')
		return
	}
	this.$http("video/attention", {
		user_id: data.userId
	}, null, (res) => {
		data.attention = (res.data == "2" ? 0 : 1);
	}, (errorCode, error) => {
		this.$toast(error)
	})
}

Vue.prototype.$commentLike = function(obj) { // 评论点赞、 取消点赞
	if (!this.$userInfo.isLogin()) {
		this.$pushToLogin('back')
		return
	}
	this.$http("videocomment/commentlike", {
		comment: obj.commentId
	}, null, (res) => {
		if (res.data == "2") {
			obj.isLike = 0;
			obj.like_num -= 1;
		} else {
			obj.isLike = 1;
			obj.like_num += 1;
		}
	}, (errorCode, error) => {
		this.$toast(error)
	})
}

Vue.prototype.$dealHeight = function(data, option, th) { //动态计算列表两列高度
	th = th ? th : 49;
	for (let i = 0; i < data.length; i++) {
		let it = data[i]
		let height = it.height * (this.width / it.width)
		if (option.left < option.right) {
			option.list1.push(it)
			option.left += height > 250 ? 250 : height + 49
		} else {
			option.list2.push(it)
			option.right += height > 250 ? 250 : height + 49
		}
	}
}

function auth() {}

Vue.prototype.$back = function(delta) {
	if (delta) {
		uni.navigateBack({
			delta: delta
		})
	} else {
		uni.navigateBack()
	}
}

Object.keys(filters).forEach(key => {
	Vue.filter(key, filters[key])
})

App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()